package com.zlm.user.partner.controller;

import com.zlm.common.base.BaseController;
import com.zlm.common.base.PageEntity;
import com.zlm.common.dto.HxGroupDto;
import com.zlm.common.dto.JsonResultDto;
import com.zlm.common.entity.UserGroupDO;
import com.zlm.user.partner.service.UserGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * @Author: duyufu
 * @Date: 2019/7/17 16:42
 */
@RestController
@RequestMapping("/group")
public class UserGroupController extends BaseController {

    @Autowired
    private UserGroupService userGroupService;

    /**
     * 创建群组
     * @param groupDto
     * @return
     */
    @PostMapping("/createGroup")
    public JsonResultDto createGroup(@RequestBody HxGroupDto groupDto) {
        try {
            String groupHxId = userGroupService.addGroup(groupDto);
            return success(groupHxId);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 批量添加群组成员
     * @param groupDto
     * @return
     */
    @PostMapping("/addHxGroupMember")
    public JsonResultDto addHxGroupMember(@RequestBody HxGroupDto groupDto) {
        try {
            userGroupService.addHxGroupMember(groupDto.getGroupHxId(),groupDto.getMembers());
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 批量删除群组成员
     * @param groupDto
     * @return
     */
    @PostMapping("/deleteHxGroupMember")
    public JsonResultDto deleteHxGroupMember(@RequestBody HxGroupDto groupDto) {
        try {
            userGroupService.deleteHxGroupMember(groupDto.getGroupHxId(),groupDto.getMembers());
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取一个用户参与的所有群组
     * @param userId
     * @return
     */
    @GetMapping("/getAllJoinedGroup")
    public JsonResultDto getAllJoinedGroup(Integer userId) {
        try {
            List<UserGroupDO> groupList = userGroupService.getAllJoinedGroup(userId);
            return success(groupList);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取群组详情
     * @param groupHxId
     * @return
     */
    @GetMapping("/getGroupDetail")
    public JsonResultDto getGroupDetail(String groupHxId) {
        try {
            UserGroupDO group = userGroupService.getGroupDetail(groupHxId);
            return success(group);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 解散群组
     * @param userId
     * @param groupHxId
     * @return
     */
    @GetMapping("/deleteHxGroup")
    public JsonResultDto deleteHxGroup(Integer userId, String groupHxId) {
        try {
            userGroupService.deleteHxGroup(userId,groupHxId);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 删除单个群组成员
     * @param groupHxId
     * @param userHxId
     * @return
     */
    @GetMapping("/deleteSingleHxGroupMember")
    public JsonResultDto deleteSingleHxGroupMember(String groupHxId, String userHxId) {
        try {
            userGroupService.deleteSingleHxGroupMember(groupHxId,userHxId);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 分页获取群组成员
     * @param groupHxId
     * @param pagenum
     * @param pagesize
     * @return
     */
    @GetMapping("/getHxGroupMember")
    public JsonResultDto getHxGroupMember(String groupHxId, Integer pagenum, Integer pagesize) {
        try {
            PageEntity page = userGroupService.getHxGroupMember(groupHxId,pagenum,pagesize);
            return success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 修改环信群组信息
     * @param userId
     * @param groupHxId
     * @param groupname
     * @param description
     * @param maxusers
     * @return
     */
    @GetMapping("/updateHxGroup")
    public JsonResultDto updateHxGroup(Integer userId,String groupHxId,String groupname,String description,
                                       Integer maxusers) {
        try {
            userGroupService.updateHxGroup(userId,groupHxId,groupname,description,maxusers);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 添加群组管理员
     * @param userId
     * @param groupHxId
     * @param userHxId
     * @return
     */
    @GetMapping("/addHxGroupAdmin")
    public JsonResultDto addHxGroupAdmin(Integer userId,String groupHxId,String userHxId) {
        try {
            userGroupService.addHxGroupAdmin(userId,groupHxId,userHxId);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 移除群组管理员
     * @param userId
     * @param groupHxId
     * @param userHxId
     * @return
     */
    @GetMapping("/deleteHxGroupAdmin")
    public JsonResultDto deleteHxGroupAdmin(Integer userId,String groupHxId,String userHxId) {
        try {
            userGroupService.deleteHxGroupAdmin(userId,groupHxId,userHxId);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 转让群组
     * @param userId
     * @param groupHxId
     * @param userHxId
     * @return
     */
    @GetMapping("/transferHxGroup")
    public JsonResultDto transferHxGroup(Integer userId,String groupHxId,String userHxId) {
        try {
            userGroupService.transferHxGroup(userId,groupHxId,userHxId);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取管理员列表
     * @param groupHxId
     * @return
     */
    @GetMapping("/getHxGroupAdmin")
    public JsonResultDto getHxGroupAdmin(String groupHxId) {
        try {
            List<Map<String,Object>> adminList = userGroupService.getHxGroupAdmin(groupHxId);
            return success(adminList);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }
}
